package cn.mujiankeji.mbrowser.界面.组件.代码框

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ClipboardManager
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.unit.dp

/**
 * 代码格式化示例页面
 */
@Composable
fun 代码格式化示例页面() {
    val 上下文 = LocalContext.current
    val 剪贴板管理器 = LocalClipboardManager.current
    
    // 示例HTML代码
    val HTML代码 = """
        <!DOCTYPE html>
        <html>
        <head>
            <title>示例页面</title>
            <meta charset="utf-8">
            <style>
                body { font-family: Arial, sans-serif; }
                .container { max-width: 800px; margin: 0 auto; }
                h1 { color: #333; }
            </style>
        </head>
        <body>
            <div class="container">
                <h1>这是一个标题</h1>
                <p>这是一个段落，包含一些<strong>加粗</strong>和<em>斜体</em>文本。</p>
                <ul>
                    <li>列表项1</li>
                    <li>列表项2</li>
                    <li>列表项3</li>
                </ul>
            </div>
        </body>
        </html>
    """.trimIndent()
    
    // 示例JSON代码
    val JSON代码 = """
        {
            "name": "示例JSON",
            "version": "1.0.0",
            "description": "这是一个示例JSON数据",
            "author": {
                "name": "开发者",
                "email": "dev@example.com"
            },
            "dependencies": {
                "lib1": "^1.0.0",
                "lib2": "^2.0.0"
            },
            "tags": [
                "示例",
                "JSON",
                "代码格式化"
            ],
            "enabled": true,
            "count": 42
        }
    """.trimIndent()
    
    var 选中标签索引 by remember { mutableStateOf(0) }
    val 标签列表 = listOf("HTML", "JSON")
    
    Scaffold(
        topBar = {
            TabRow(selectedTabIndex = 选中标签索引) {
                标签列表.forEachIndexed { index, title ->
                    Tab(
                        selected = 选中标签索引 == index,
                        onClick = { 选中标签索引 = index },
                        text = { Text(title) }
                    )
                }
            }
        }
    ) { paddingValues ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(paddingValues)
        ) {
            Text(
                text = "代码格式化示例",
                style = MaterialTheme.typography.headlineSmall,
                modifier = Modifier.padding(16.dp)
            )
            
            Text(
                text = "点击代码项可以展开/折叠，长按可以复制内容",
                style = MaterialTheme.typography.bodyMedium,
                modifier = Modifier.padding(horizontal = 16.dp)
            )
            
            Spacer(modifier = Modifier.height(8.dp))
            
            when (选中标签索引) {
                0 -> {
                    // HTML格式化视图
                    代码格式化视图(
                        代码 = HTML代码,
                        类型 = 代码格式化类型.HTML,
                        modifier = Modifier
                            .fillMaxWidth()
                            .weight(1f),
                        项目选中回调 = { 项 ->
                            // 复制文本到剪贴板
                            项.文本?.let { 文本 ->
                                剪贴板管理器.setText(文本)
                                Toast.makeText(上下文, "已复制到剪贴板", Toast.LENGTH_SHORT).show()
                            }
                        }
                    )
                }
                1 -> {
                    // JSON格式化视图
                    代码格式化视图(
                        代码 = JSON代码,
                        类型 = 代码格式化类型.JSON,
                        modifier = Modifier
                            .fillMaxWidth()
                            .weight(1f),
                        项目选中回调 = { 项 ->
                            // 复制文本到剪贴板
                            项.文本?.let { 文本 ->
                                剪贴板管理器.setText(文本)
                                Toast.makeText(上下文, "已复制到剪贴板", Toast.LENGTH_SHORT).show()
                            }
                        }
                    )
                }
            }
        }
    }
} 